package com.loongtech.bi.action.system;

import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.loongtech.bi.support.Log;
import com.loongtech.core.util.Time;
	
/**
 * 数据修复
 * @author HivenYang
 *	@date 2015年2月15日
 */
@Controller
@Scope("view")
public class DataRepairAction {
	/*@Autowired
	private UserManager						userManager;
	@Autowired
	private PayAnalysisManager				payAnalysisManager;
	@Autowired
	private GameDataAnalysisManager			gameDataAnalysisManager;
	@Autowired
	private OnlineNumAnalysisManager		onlineNumAnalysisManager;
	@Autowired
	private ClientNewDataAnalysisManager	clientNewDataAnalysisManager;
	@Autowired
	private AliveDayAliveManager			aliveDayAliveManager;
	@Autowired
	private AliveMonthAliveManager			aliveMonthAliveManager;
	@Autowired
	private AliveWeekAliveManager			aliveWeekAliveManager;
	@Autowired
	private RegisterDayAliveManager			registerDayAliveManager;
	@Autowired
	private RegisterWeekAliveManager		registerWeekAliveManager;
	@Autowired
	private RegisterMonthAliveManager		registerMonthAliveManager;
	@Autowired
	private LoginFrequencyDayManager		loginFrequencyDayManager;
	@Autowired
	private TimerManagerParser				timerManagerParser;
	@Autowired
	private DeviceManager deviceManager;
	@Autowired
	private DeviceAliveManager deviceAliveManager;*/
	// 需要修复数据的日期
	private Date							date	= Time.currentDate();
	private String							type;
	private Map<String, String>				module	= new LinkedHashMap<>();

	@PostConstruct
	private void init()
	{
		for (Type t : Type.values())
		{
			module.put(t.name(), t.desc);
		}
	}

	public void repair()
	{
		FacesContext context = FacesContext.getCurrentInstance();
		if (StringUtils.isBlank(type) || !module.containsKey(type))
		{
			context.addMessage(null, new FacesMessage("数据类型不存在", "数据类型不存在"));
			return;
		}
		//long time = date.getTime();
		Type t = Type.valueOf(type);
		try
		{
			/*switch (t)
			{
			case INPUT_DATA:
				//timerManagerParser.parseLog();
				break;
			case ONLINE_NUM:
				onlineNumAnalysisManager.analyse(time);
				break;				
			case USER_REGISTER:
				userManager.analyseUser(time);
				break;
			case ACTIVATE_CODE:
				userManager.analyseActivateCode(time);
				break;
			case REGISTER_ALIVE_DAY:
				registerDayAliveManager.analyse(time);
				break;
			case REGISTER_ALIVE_WEEK:
				registerWeekAliveManager.analyse(time);
				break;
			case REGISTER_ALIVE_MONTH:
				registerMonthAliveManager.analyse(time);
				break;
			case ALIVE_ALIVE_DAY:
				aliveDayAliveManager.analyse(time);
				break;
			case ALIVE_ALIVE_WEEK:
				aliveWeekAliveManager.analyse(time);
				break;
			case ALIVE_ALIVE_MONTH:
				aliveMonthAliveManager.analyse(time);
				break;
			case LOGIN_FREQUENCY_DAY:
				loginFrequencyDayManager.analyse(time);
				break;
			case CLIENT_ACTIVATE:
				clientNewDataAnalysisManager.analyseClientActivate(time);
				break;
			case CLIENT_SDK_LOGIN:
				clientNewDataAnalysisManager.analyseSdkLogin(time);
				break;
			case PAY_DAY:
				payAnalysisManager.analysePayDay(time);
				break;
			case GAME_LOGIN:
				clientNewDataAnalysisManager.analyseGameLogin(time);
				break;
			case GAME_DATA_DAY:
				gameDataAnalysisManager.analyse(time);
				break;
			case DEVICE_REGISTER:
				deviceManager.analyseDevice(time);
				break;
			case DEVICE_REGISTER_ALIVE_DAY:
				deviceAliveManager.analyseRegisterDayAlive(time);
				break;
			case DEVICE_ALIVE_ALIVE_DAY:
				deviceAliveManager.analyseAliveDayAlive(time);
				break;
			}*/
		}
		catch (Exception e)
		{
			if (Log.bi.isErrorEnabled())
			{
				Log.bi.error("修复{}数据发生异常", t, e);
			}
			context.addMessage(null, new FacesMessage("数据修复失败", "数据修复发生异常"));
		}
		context.addMessage(null, new FacesMessage("数据修复成功", "数据修复成功"));
	}
	
	private enum Type{
		INPUT_DATA("原始数据记录"),
		ONLINE_NUM("在线数据"),
		USER_REGISTER("用户新增"),            
		ACTIVATE_CODE("激活码使用"),           
		REGISTER_ALIVE_DAY("账号新增日留存"),
		REGISTER_ALIVE_WEEK("账号新增周留存"),     
		REGISTER_ALIVE_MONTH("账号新增月留存"),    
		ALIVE_ALIVE_DAY("账号活跃日留存"),         
		ALIVE_ALIVE_WEEK("账号活跃周留存"),        
		ALIVE_ALIVE_MONTH("账号活跃月留存"),       
		LOGIN_FREQUENCY_DAY("登录频次"),      
		CLIENT_ACTIVATE("客户端新增激活"),     
		CLIENT_SDK_LOGIN("客户端登录SDK成功"),   
		PAY_DAY("日充值数据"),
		GAME_LOGIN("游戏登录数据"),             
		GAME_DATA_DAY("游戏数据"),
		DEVICE_REGISTER("设备新增"),
		DEVICE_REGISTER_ALIVE_DAY("设备新增日留存"),
		DEVICE_ALIVE_ALIVE_DAY("设备活跃日留存"),         
		;
		
		private String desc;
		
		private Type(String desc) {
			this.desc = desc;
		}
	}

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public Map<String, String> getModule() {
		return module;
	}

	public void setModule(Map<String, String> module) {
		this.module = module;
	}
}